package org.yoki.edu.bg.radius.filter.config.radius;

import org.yoki.edu.bg.radius.filter.app.receiver.AcctRadiusPacketReceiver;
import org.yoki.edu.bg.radius.filter.app.receiver.CheckRadiusPacketReceiver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author Sky$
 * @Description: TODO
 * @date 2018/1/4$ 16:24$
 */
@Configuration
@EnableConfigurationProperties(ReceiverThreadPoolProperties.class)
public class RadiusConfig {

    @Autowired
    private ReceiverThreadPoolProperties poolProperties;

    @Bean
    public ThreadPoolExecutor receiverThreadPoolExecutor() {
        ThreadPoolExecutor executor;
        BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>(poolProperties.getQueueSize());
        executor = new ThreadPoolExecutor(poolProperties.getCorePollSize(), poolProperties.getMaximumPoolSize(), poolProperties.getKeepAliveTime(),
                TimeUnit.MILLISECONDS, workQueue,
                new ThreadPoolExecutor.AbortPolicy());
        return executor;
    }

    @Bean
    @DependsOn(value = {"receiverThreadPoolExecutor"})
    public AcctRadiusPacketReceiver acctPacketReceiver(ThreadPoolExecutor receiverThreadPoolExecutor) {
        AcctRadiusPacketReceiver receiver = new AcctRadiusPacketReceiver();
        receiver.setExecutor(receiverThreadPoolExecutor);
        return receiver;
    }

    @Bean
    @DependsOn(value = {"receiverThreadPoolExecutor"})
    public CheckRadiusPacketReceiver checkPacketReceiver(ThreadPoolExecutor receiverThreadPoolExecutor) {
        CheckRadiusPacketReceiver receiver = new CheckRadiusPacketReceiver();
        receiver.setExecutor(receiverThreadPoolExecutor);
        return receiver;
    }



}
